var $mainbody = $("#mainbody");
//===============品牌页面元素start===============
var $addBrandBtn = $('#addBrandBtn');
var $brandName = $('#brandName');
var $staffDialogClose = $('#staffDialogClose');
var $brandDialog = $("#staffDialog");
//===============品牌页面元素end=================


var $userSearch = $('#userSearch');//输入关键字查询

//分页信息
var staffInfo = {};
staffInfo.data = {
    pageSize:10,//每页记录数
    totalSize:0,//总记录数
    curPage:1,//当前页 1..n
    totalPage:0//总页数
};

//分页操作
staffInfo.page = {
    /**
     * @description 转到第一页
     * @author
     */
    turnFirst : function() {
        if(staffInfo.data.curPage == 1){
            promptMessge('已经是第一页',1000);
        }else{
            staffInfo.data.curPage = 1;
            //QueryResult();
        }
    },
    /**
     * @description 转到尾页
     * @author
     */
    turnEnd : function() {
        if(staffInfo.data.curPage == staffInfo.data.totalPage){
            promptMessge('已经是最后一页',1000);
        } else {
            staffInfo.data.curPage = staffInfo.data.totalPage;
            //QueryResult();
        }
    },
    /**
     * @description 转到上页
     * @author
     */
    turnPre : function() {
        if (staffInfo.data.curPage != 1) {
            staffInfo.data.curPage -= 1;
            //QueryResult();
        } else {
            promptMessge('已经是第一页',1000);
        }
    },
    /**
     * @description 转到下页
     * @author
     */
    turnNext : function() {
        if (staffInfo.data.curPage+1 <= staffInfo.data.totalPage) {
            staffInfo.data.curPage += 1;
            //QueryResult();
        } else {
            promptMessge('已经是最后一页',1000);
        }
    },
    //container 容器，count 总页数 pageindex 当前页数
    setPage : function (container, count, pageindex) {
        var container = container;
        var count = count;
        var pageindex = pageindex;
        var a = [];
        //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
        if (pageindex == 1) {
            a[a.length] = '<li class="disabled"><a>&lt;</a></li>';
        } else {
            a[a.length] = '<li><a>&lt;</a></li>';
        }
        function setPageList() {
            if (pageindex == i) {
                a[a.length] = '<li class="active"><a>' + i + '</a></li>';
            } else {
                a[a.length] = "<li><a>" + i + "</a></li>";
            }
        }

        //总页数小于10
        if (count <= 10) {
            for (var i = 1; i <= count; i++) {
                setPageList();
            }
        } else {
            //总页数大于10页
            if (pageindex <= 4) {
                for (var i = 1; i <= 5; i++) {
                    setPageList();
                }
                a[a.length] = '<li class="disabled"><a>...</a></li><li><a>' + count + '</a></li>';
            } else if (pageindex >= count - 3) {
                a[a.length] = '<li><a>1</a></li><li class="disabled"><a>...</a></li>';
                for (var i = count - 4; i <= count; i++) {
                    setPageList();
                }
            }
            else { //当前页在中间部分
                a[a.length] = '<li><a>1</a></li><li class="disabled"><a>...</a></li>';
                for (var i = pageindex - 2; i <= pageindex + 2; i++) {
                    setPageList();
                }
                a[a.length] = '<li><a>...</a></li><li><a>' + count + '</a></li>';
            }
        }
        if (pageindex == count) {
            a[a.length] = '<li class="disabled"><a>&gt; </a></li>';
        } else {
            a[a.length] = "<li><a>&gt; </a></li>";
        }
        container.innerHTML = a.join("");

        //事件点击
        (function () {
            var oAlink = container.getElementsByTagName("a");
            var inx = pageindex; //初始的页码
            oAlink[0].onclick = function () { //点击上一页
                if (inx == 1) {
                    return false;
                }
                inx--;
                staffInfo.page.setPage(container, count, inx);
                staffInfo.data.curPage = inx;
                //展示销售品列表
                staffInfo.queryList();
                return false;
            }
            for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
                if(i === staffInfo.data.curPage){
                    continue;
                }
                oAlink[i].onclick = function () {
                    inx = parseInt(this.innerHTML);
                    staffInfo.page.setPage(container, count, inx);
                    staffInfo.data.curPage = inx;
                    //展示销售品列表
//			    staffInfo.layOut.renderOfferList(staffInfo.data.categoryNodeId);
                    staffInfo.queryList();
                    return false;
                }
            }
            oAlink[oAlink.length - 1].onclick = function () { //点击下一页
                if (inx == count) {
                    return false;
                }
                inx++;
                staffInfo.page.setPage(container, count, inx);
                staffInfo.data.curPage = inx;
                //展示销售品列表
//			  staffInfo.layOut.renderOfferList(staffInfo.data.categoryNodeId);
                staffInfo.queryList();
                return false;
            }
        })();
    }
};

/**
 * 设置页码
 */
staffInfo.renderPageInfo = function () {
    $(".pagination_c").empty();
    $(".pagination_c").append(
        '<ul class="pagination"></ul>'
    );
    staffInfo.page.setPage($(".pagination_c").find(".pagination")[0], staffInfo.data.totalPage, staffInfo.data.curPage);
};

/**
 * 获取用户数据
 */
staffInfo.queryList= function(){
    var param = {
        cPage: staffInfo.data.curPage,
        pSize: staffInfo.data.pageSize,
        typeName:$userSearch.val().trim()
    };
    Ajax.ajax({
        url: "/type/gryTypeList",
        data: param,
        type: "post",
        dataType: 'json',
        success:function(res){
            if (res.resultCode == 0) {
                staffInfo.data.totalSize = res.data.totalRow;
                staffInfo.data.totalPage = res.data.totalPage;
                staffInfo.renderPageInfo();
                $(".page-pageno").html(staffInfo.data.curPage);
                $(".page-totalcount").html(staffInfo.data.totalSize);
                $(".page-pageno").html(staffInfo.data.curPage);
                $(".page-pagecount").html(staffInfo.data.totalPage);
                staffInfo.showTabel(res.data.list);
            }
        }

    });
}

/**
 * 展示用户数据
 * @param list
 */
staffInfo.showTabel= function(list){
    $mainbody.empty();

    $.each(list,function(n,v) {
        var $tr = $("<tr>");
        var $checkbox = $('<td><input type="checkbox" data-id="' + v.id + '"></td>');
        var $ordr = $('<td>' + (n+1) + '</td>');

        var $name = $('<td>' + v.offer_type_name + '</td>');

        var btnClass = 'btn btn-primary btn-trans btn-xs';
        var $button3 = $('<button class="'+btnClass+' btn-op" data-id="' + v.id + '" data-name="' + v.offer_type_name + '"> 编辑</button>');
        var $button4 = $('<button class="'+btnClass+' btn-del" data-id="' + v.id + '">删除</button>');

        var $btnTd = $('<td>').append($button3,$button4);
        $tr.append($checkbox, $ordr,$name, $btnTd);
        $mainbody.append($tr);
    });
    //编辑 按钮事件设置
    $('button.btn-op',$mainbody).on('click',function () {
        var $this = $(this);
        $brandName.val($this.attr("data-name"));
        var id = $this.attr("data-id");
        $brandDialog.attr("data-id",id);
        $brandDialog.show();
    });

    //删除按钮事件
    $('button.btn-del',$mainbody).on('click',function () {
        var $this = $(this);
        if(confirm('确定删除?')){
            deleteBrand($this.attr("data-id"));
        }
    })

    //选中当前行事件设置
    $('tr', $mainbody).on('click',function (e) {
        var $checkbox = $(this).find(':checkbox');
        if(e.target != $checkbox[0]){
            $checkbox.click();
        }
    });
}
function addType() {
    var brandName = $brandName.val();
    if(brandName == ""){
        alert("请输入品牌名称");
        return;
    }
    var param = {
        brandName:brandName
    }
    Ajax.ajax({
        url: "/type/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                // alert(res.data);
                clearDialog();
                staffInfo.queryList();
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}
function deleteBrand(id) {
    var param = {
        id:id
    }
    Ajax.ajax({
        url: "/type/delete",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                // alert(res.data);
                staffInfo.queryList();
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}
function updateBrand(id) {
    var offerTypeName = $brandName.val();
    if(offerTypeName == ""){
        alert("请输入品牌名称");
        return;
    }
    var param = {
        id:id,
        offerTypeName:offerTypeName
    }
    Ajax.ajax({
        url: "/type/update",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                // alert(res.data);
                clearDialog();
                staffInfo.queryList();
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function clearDialog() {
    $brandName.val("");
    $brandDialog.attr("data-id","");
    $brandDialog.hide();
}
$(function(){

    //新增品牌绑定事件
    $addBrandBtn.on('click',function () {
        debugger;
        if($brandDialog.attr("data-id") == "" || $brandDialog.attr("data-id") == undefined){
            addType();
        }else {
            updateBrand($brandDialog.attr("data-id"));
        }

    });

    $staffDialogClose.on('click',function () {
        clearDialog();
    });
    $('#btnAdd').on('click',function(){
        $brandDialog.show();
    })

    staffInfo.queryList();

    //每页显示的数据数切换
    $(".page-pagesize").change(function(){
        staffInfo.data.pageSize = $(this).val();
        staffInfo.data.curPage=1;
        staffInfo.queryList();
    });

    //查找按钮
    $('#userSearchBtn').on('click',function(){
        staffInfo.data.curPage = 1;
        staffInfo.queryList();
    });

    $('#btnAdd').on('click',function(){
        $brandDialog.show();
    })
})
